package com.apex.restful.service.impl;

import com.apex.restful.domain.TBsdtWxConfigRepository;
import com.apex.restful.entity.TBsdtWxUser;
import com.apex.restful.service.TBsdtWxConfigService;
import com.apex.util.ResponseMessage;
import com.edu.orm.annotation.TargetDataSource;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * user:wangj
 * date:2018/4/8
 * Time: 21:34
 */
@Service
public class TBsdtWxConfigServiceImpl implements TBsdtWxConfigService {

    @Autowired
    private TBsdtWxConfigRepository repository;

    @TargetDataSource("ds1")
    @Transactional
    @Override
    public String save(String code) {
        Map<String, Object> map = ResponseMessage.getMap();
        if (StringUtils.isNotBlank(code)) {
            Any any = JsonIterator.deserialize(code);
            if (null != any) {
                String data = any.get("data").toString();
                if (StringUtils.isNotBlank(data)) {
                    TBsdtWxUser user = any.get("data").as(TBsdtWxUser.class);
                    if (null != user && StringUtils.isNotBlank(user.getOpenid())) {
                        repository.save(user);
                    }
                    map = ResponseMessage.getTrueMap();
                    map.put("data", user);
                }
            }
        }
        return JsonStream.serialize(map);
    }

    @TargetDataSource("ds1")
    @Override
    public String login(String cookieId) {
        Map<String, Object> map = ResponseMessage.getMap();
        if (StringUtils.isNotBlank(cookieId)) {
            TBsdtWxUser user = repository.findByCookie(cookieId);
            if (null != user) {
                map = ResponseMessage.getTrueMap();
                map.put("data", user);
            }
        }
        return JsonStream.serialize(map);
    }

    @TargetDataSource("ds1")
    @Transactional
    @Override
    public String update(String code) {
        Map<String, Object> map = ResponseMessage.getMap();
        if (StringUtils.isNotBlank(code)) {
            Any any = JsonIterator.deserialize(code);
            if (null != any) {
                String data = any.get("data").toString();
                if (StringUtils.isNotBlank(data)) {
                    TBsdtWxUser user = any.get("data").as(TBsdtWxUser.class);
                    if (null != user && StringUtils.isNotBlank(user.getUserid())) {
                        List<TBsdtWxUser> list = repository.getAllByUserid(user.getUserid());
                        if (null != list && list.size() > 0) {
                            TBsdtWxUser user1 = list.get(0);
                            user.setOpenid(user1.getOpenid());
                            repository.save(user);
                        }
                    }
                    map = ResponseMessage.getTrueMap();
                    map.put("data", user);
                }
            }
        }
        return JsonStream.serialize(map);
    }
}
